#pragma once
#include "stdafx.h"
#include "one.h"



#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>



///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////




int bar(IplImage* frame,int threshold)
{
int bb=0;
int codebar=0;
char codeb[6];
	
int gray;
int i=0;
int j=0;

int max_gray = 0;
int min_gray = -1;


for(j=0;j<frame->width;j++) {
for (i=0;i<frame->height;i++){
         gray=(((uchar*)(frame->imageData + frame->widthStep*i))[j*3]+
         ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+1]+
         ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+2])/3;

         ((uchar*)(frame->imageData + frame->widthStep*i))[j*3] = gray;
         ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+1] = gray;
         ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+2] = gray;
	
    	 if (gray > max_gray) max_gray = gray;
    	 if (gray < min_gray) min_gray = gray;

         }}



//////////////////////////////////////////////////////////////////
//               Local variables                             //
//////////////////////////////////////////////////////////////////



int threshold_gray = (max_gray + min_gray) /2;

int black[32];
int white[32];
int k=0;
int b=0;
int w=0;
int z=0;
int Bbin[32];
int Wbin[32];
int t1[32];
int t2[32];
int t3[32];
int t4[32];
float T[32];
//initialise arays to 0
for(k=0;k<32;k++){black[k]=0;white[k]=0;Bbin[k]=0;Wbin[k]=0;}
for(k=0;k<32;k++){t1[k]=0;t2[k]=0;t3[k]=0;t4[k]=0;T[k]=0;}

//////////////////////////////////////////////////////////////////
//               Convert image to binary              //
//////////////////////////////////////////////////////////////////

 

for(j=0;j<frame->width;j++) {
for (i=0;i<frame->height;i++){
                 
      if (((uchar*)(frame->imageData + frame->widthStep*i))[j*3] < threshold )
      ((uchar*)(frame->imageData + frame->widthStep*i))[j*3] = 0;

      else
      ((uchar*)(frame->imageData + frame->widthStep*i))[j*3] = 255; 

      if (((uchar*)(frame->imageData + frame->widthStep*i))[j*3+1] < threshold)
	  ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+1] = 0;

	  else 
 	  ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+1] = 255;

      if (((uchar*)(frame->imageData + frame->widthStep*i))[j*3+2] < threshold)
      ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+2] = 0;

      else
	  ((uchar*)(frame->imageData + frame->widthStep*i))[j*3+2] = 255;

	
 	  }}



///////////////////////////////////////////////////////////////////////////////
//              BARCODE // 
///////////////////////////////////////////////////////////////////////////////
 

for(j=0;j<frame->width;j++) {

//count number of black and white lines along width of image

      if (((uchar*)(frame->imageData + frame->widthStep*((frame->height)/2)))[j*3] == 255 && ((uchar*)(frame->imageData + frame->widthStep*((frame->height)/2)))[j*3+3] == 0)
	  {b=b+1;}
      if (((uchar*)(frame->imageData + frame->widthStep*((frame->height)/2)))[j*3] == 0 && ((uchar*)(frame->imageData + frame->widthStep*((frame->height)/2)))[j*3+3] == 255)
	  {w=w+1;}
                
//calcuolate thickness of each line
	
     if (((uchar*)(frame->imageData + frame->widthStep*((frame->height)/2)))[j*3] == 255){
	 white[w]=white[w]+1;}
     else{
	 black[b]=black[b]+1; }
	 }   
	 

//calculate t values

for(z=1;z<12;z=z+2){
	 t1[z]=black[z+2]+white[z+1];	
	 t2[z]=black[z+3]+white[z+1];
	 t3[z]=black[z+3]+white[z+2];
	 t4[z]=white[z+2];
   	 T[z]=black[z+2]+white[z+1]+black[z+3]+white[z+2];
	 }
//calculate binary values

for(z=1;z<12;z=z+2){
	 if(black[z+2]<=(T[z]/7)+((T[z]/7)/2) && black[z+2]>0 ){Bbin[z]=1;}
	 if(black[z+2]<=(T[z]/7)*2+((T[z]/7)/2) && black[z+2]>(T[z]/7)*2-((T[z]/7)/2) ){Bbin[z]=11;}
	 if(black[z+2]<=(T[z]/7)*3+((T[z]/7)/2) && black[z+2]>(T[z]/7)*3-((T[z]/7)/2) ){Bbin[z]=111;}
	 if(black[z+2]<=(T[z]/7)*4+((T[z]/7)/2) && black[z+2]>(T[z]/7)*4-((T[z]/7)/2) ){Bbin[z]=1111;}

     if(black[z+3]<=(T[z]/7)+((T[z]/7)/2) && black[z+3]>0 ){Bbin[z+1]=1;}
	 if(black[z+3]<=(T[z]/7)*2+((T[z]/7)/2) && black[z+3]>(T[z]/7)*2-((T[z]/7)/2) ){Bbin[z+1]=11;}
	 if(black[z+3]<=(T[z]/7)*3+((T[z]/7)/2) && black[z+3]>(T[z]/7)*3-((T[z]/7)/2) ){Bbin[z+1]=111;}
	 if(black[z+3]<=(T[z]/7)*4+((T[z]/7)/2) && black[z+3]>(T[z]/7)*4-((T[z]/7)/2) ){Bbin[z+1]=1111;}

	 }
for(z=1;z<12;z=z+2){
	 if(white[z+1]<=(T[z]/7)+((T[z]/7)/2) && white[z+1]>0 ){Wbin[z]=1;}
	 if(white[z+1]<=(T[z]/7)*2+((T[z]/7)/2)&& white[z+1]>(T[z]/7)*2-((T[z]/7)/2) ){Wbin[z]=11;}
	 if(white[z+1]<=(T[z]/7)*3+((T[z]/7)/2) && white[z+1]>(T[z]/7)*3-((T[z]/7)/2) ){Wbin[z]=111;}
	 if(white[z+1]<=(T[z]/7)*4+((T[z]/7)/2) && white[z+1]>(T[z]/7)*4-((T[z]/7)/2) ){Wbin[z]=1111;}

     if(white[z+2]<=(T[z]/7)+((T[z]/7)/2) && white[z+2]>0 ){Wbin[z+1]=1;}
	 if(white[z+2]<=(T[z]/7)*2+((T[z]/7)/2) && white[z+2]>(T[z]/7)*2-((T[z]/7)/2) ){Wbin[z+1]=11;}
	 if(white[z+2]<=(T[z]/7)*3+((T[z]/7)/2) && white[z+2]>(T[z]/7)*3-((T[z]/7)/2) ){Wbin[z+1]=111;}
	 if(white[z+2]<=(T[z]/7)*4+((T[z]/7)/2) && white[z+2]>(T[z]/7)*4-((T[z]/7)/2) ){Wbin[z+1]=1111;}


	 }
char stry1[1];
char Y;
CvFont font;
double hscale=1; //use 1 for mine
double vscale=1; //use 1 for mine
int lineWidth=3; //use 1 for mine
cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC,hscale,vscale,0,lineWidth);

 


//calculate and display barcode decimal number
bb=0;
for(z=1;z<12;z=z+2){


	//odd//
	if(Wbin[z]==111&&Bbin[z]==11&&Bbin[z+1]==1&&Wbin[z+1]==1){;Y='0'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
 	 if(Wbin[z]==11&&Bbin[z]==11&&Bbin[z+1]==1&&Wbin[z+1]==11){;Y='1'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==11&&Bbin[z]==1&&Bbin[z+1]==11&&Wbin[z+1]==11){;Y='2'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==1111&&Bbin[z+1]==1&&Wbin[z+1]==1){;Y='3'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==1&&Bbin[z+1]==11&&Wbin[z+1]==111){;Y='4'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==11&&Bbin[z+1]==1&&Wbin[z+1]==111){;Y='5'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==1&&Bbin[z+1]==1111&&Wbin[z+1]==1){;Y='6'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==111&&Bbin[z+1]==11&&Wbin[z+1]==1){;Y='7'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==11&&Bbin[z+1]==111&&Wbin[z+1]==1){;Y='8'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==111&&Bbin[z]==1&&Bbin[z+1]==11&&Wbin[z+1]==1){Y='9'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
  //even//
     if(Wbin[z]==1&&Bbin[z]==1&&Bbin[z+1]==111&&Wbin[z+1]==11){Y='0'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
 	 if(Wbin[z]==1&&Bbin[z]==11&&Bbin[z+1]==11&&Wbin[z+1]==11){Y='1'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==11&&Bbin[z]==11&&Bbin[z+1]==11&&Wbin[z+1]==1){Y='2'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==1&&Bbin[z+1]==1&&Wbin[z+1]==1111){Y='3'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==11&&Bbin[z]==111&&Bbin[z+1]==1&&Wbin[z+1]==1){Y='4'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1&&Bbin[z]==111&&Bbin[z+1]==1&&Wbin[z+1]==11){Y='5'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==1111&&Bbin[z]==1&&Bbin[z+1]==1&&Wbin[z+1]==1){Y='6'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==11&&Bbin[z]==1&&Bbin[z+1]==1&&Wbin[z+1]==111){Y='7'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==111&&Bbin[z]==1&&Bbin[z+1]==1&&Wbin[z+1]==11){Y='8'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
	 if(Wbin[z]==11&&Bbin[z]==1&&Bbin[z+1]==111&&Wbin[z+1]==1){Y='9'; 	sprintf(stry1,"%c",Y); cvPutText(frame,stry1,cvPoint(9*z,50),&font,cvScalar(0,0,255));}
		  
	 codeb[bb]=Y;
	 bb=bb+1;
	 }
   
codebar= atoi(codeb);

return(codebar);

      w=-1;b=0;






}
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////